//
//  DreamTravelViewController.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/11.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "DreamTravelViewController.h"
#import "AppTools.h"
#import "YMDTListCityModel.h"
#import "YYMDreamTravelListCell.h"
#import "YMDTDetailsViewController.h"
#import "YYMSearchViewController.h"

@interface DreamTravelViewController () <UITableViewDataSource, UITableViewDelegate, tapDelegate>

@property (strong, nonatomic) UITableView *selectTable;

@property (strong, nonatomic) NSMutableArray *selectArray;

@property (nonatomic, copy) NSString *cityId;

@property (nonatomic, strong) NSMutableArray *dataArray;
- (IBAction)searchAction:(id)sender;
- (IBAction)selectAction:(id)sender;

@end

@implementation DreamTravelViewController

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.dataArray = [NSMutableArray array];
    
    NSArray *array = [AppTools getArrayFromFile:@"dtravel"];
    
    _selectArray = [NSMutableArray array];
    
    for (NSDictionary *dic in array) {
        
        YMDTListCityModel *model = [[YMDTListCityModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        
        [self.dataArray addObject:model];
    }
    
    self.searchButton.layer.masksToBounds = YES;
    self.searchButton.layer.cornerRadius = 12;
    self.searchButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.searchButton.layer.borderWidth = 1.5;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _cityId = @"";
    
    [self afnGetData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == _selectTable) {
        return _selectArray.count;
    }
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == _selectTable) {
        
        static NSString *cellIndentifier = @"cell2";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        }
        
        NSDictionary *dic = [_selectArray objectAtIndex:indexPath.row];
        
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.text = [dic objectForKey:@"name"];
        
        return cell;
    }
    
    static NSString *cellIndentifier = @"cell";
    
    YYMDreamTravelListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    YMDTListCityModel *model = [_dataArray objectAtIndex:indexPath.row];
    
    if (!cell) {
        cell = [[YYMDreamTravelListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        cell.delegate = self;
    }
    
    cell.model = model;
    cell.tag = indexPath.row + 1000;
    
    return cell;
}

- (void)afnGetData{
    
    NSString *http = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/getCityList?cid=%@&_tk=1447291434:4474&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A9641", _cityId];
    
    [AppTools getAFN:http andpara:nil andBlock:^(id result) {
        
        [self.dataArray removeAllObjects];
        
        NSArray *array = [result objectForKey:@"data"];
        
        NSArray *array2 = [result objectForKey:@"continentList"];
        
        for (NSDictionary *dic in array2) {
            
            [_selectArray addObject:dic];
            
        }
        
        [AppTools saveArrayToFile:@"dtravel" andArray:array];
        
        for (NSDictionary *dic in array) {
            YMDTListCityModel *model = [[YMDTListCityModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView reloadData];
    } andfailBlock:^(id result) {
        
        
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == _selectTable) {
        return 44;
    }
    
    return HEIGHT / 2.3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = [_selectArray objectAtIndex:indexPath.row];
    
    NSString *cityId = [dic objectForKey:@"id"];
    
    if ([_cityId isEqualToString:cityId]) {
        return;
    }else{
        _cityId = cityId;
        [_tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
    [self afnGetData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == _selectTable) {
        return;
    }
    
    _selectTable.frame = CGRectMake(-100, 64, 100, HEIGHT);
    
    NSArray *array = _tableView.indexPathsForVisibleRows;
    CGFloat content = scrollView.contentOffset.y;
    
    for (NSIndexPath *indexPath in array) {
        
        YYMDreamTravelListCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
        CGRect contentImageViewRect = cell.mainImageView.frame;
        contentImageViewRect.origin.y = 0 + (content - indexPath.row * [UIScreen mainScreen].bounds.size.height / 2.3) / 3;
        cell.mainImageView.frame = contentImageViewRect;
        
    }
}

- (void)delegateDelegate:(NSInteger)index{
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    YMDTListCityModel *model = [_dataArray objectAtIndex:index];
    YYMDreamTravelListCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    
    YMDTDetailsViewController *detalsVC = [[YMDTDetailsViewController alloc] init];
    
    detalsVC.frame = cell.frame;
    detalsVC.image = cell.mainImageView.image;
    detalsVC.cityID = model.iD;
    self.tabBarController.tabBar.hidden = YES;
    
    [self.navigationController pushViewController:detalsVC animated:YES];
}

- (IBAction)searchAction:(id)sender {
    
    YYMSearchViewController *vc = [[YYMSearchViewController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (IBAction)selectAction:(id)sender {
    
    if (_selectTable == nil) {
        _selectTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 100, HEIGHT)];
        _selectTable.backgroundColor = [UIColor clearColor];
        _selectTable.dataSource = self;
        _selectTable.delegate = self;
        
        [self.view addSubview:_selectTable];
    }else if(_selectTable != nil && _selectTable.frame.origin.x == -100){
        _selectTable.frame = CGRectMake(0, 64, 100, HEIGHT);
    }else{
        _selectTable.frame = CGRectMake(-100, 64, 100, HEIGHT);
    }
}

@end
